package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleLocationData;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleLocationInfo;
import com.tianhui.consignortwofei.utils.Transform;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2020/9/28
 * @description
 **/
public class VehicleLocationActivity extends BaseActivity {
    @BindView(R.id.layout_map_view_bd_mapView)
    MapView mMapView;
    private BaiduMap mBaiDuMap;
    private OrderModel mOrderModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vehicle_location;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "实时定位";
    }

    @Override
    public void init() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            String plateNumber = bundle.getString(KeyValue.KEY_PLATE_NUMBER);
            requestVehicleLocation(plateNumber);
        }

        mBaiDuMap = mMapView.getMap();


//        mBaiDuMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
//            @Override
//            public void onMapLoaded() {
//                LatLng latLng = new LatLng(35.11882666666666, 118.18414333333334);
//                showVehicleMapLocation(latLng);
//            }
//        });

    }

    private void showVehicleMapLocation(LatLng latLng) {

        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng, 10);
        mBaiDuMap.setMapStatus(mapStatusUpdate);
        MarkerOptions startMarker = new MarkerOptions()
                .position(latLng)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_vehicle_track_position));
        mBaiDuMap.addOverlay(startMarker);
    }

    private void requestVehicleLocation(String plateNumber) {
        Map<String, String> map = new HashMap<>();
        map.put("vclN", plateNumber);
        mOrderModel = new OrderModel();
        mOrderModel.vehicleLocationModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        VehicleLocationData locationData = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), VehicleLocationData.class);
                        if (locationData != null) {
                            VehicleLocationInfo locationInfo = locationData.result;
                            if (locationInfo != null) {
                                double lat = StringUtils.toDouble(locationInfo.lat) / 600000;
                                double lng = StringUtils.toDouble(locationInfo.lon) / 600000;

                                LatLng latLng = Transform.wgs84tobd09(lng, lat);
                                showVehicleMapLocation(latLng);
                            }
                        } else {
                            showToast("暂无位置信息");
                        }
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

                showToast(e.message);
            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    protected void onResume() {
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}
